const eventproxy = require('eventproxy')
const superagent = require('superagent')
const cheerio = require('cheerio')

const url = require('url')
const cnodeUrl = 'https://cnodejs.org/'


superagent.get(cnodeUrl).end(function (err, res) {
  if (err) return console.error(err);


  let topicUrls = []

  let $ = cheerio.load(res.text)

  $('#topic_list .topic_title').each((k, v) => {
    let $a = $(v)
    topicUrls.push(url.resolve(cnodeUrl, $a.attr('href')))
  })


  let ep = new eventproxy()

  // 命令ep重复监听数组的长度

  ep.after('topic_html', topicUrls.length, (topics) => {
    // topics是个数组 包含40次ep.emit('topic_html',pair)

    topics = topics.map((topicPair) => {
      let topicUrl = topicPair[0]
      let topicHtml = topicPair[1]
      let $ = cheerio.load(topicHtml)
      return ({
        title: $('.topic_full_title').text().trim(),
        href: topicUrl,
        comment1: $('.reply_content p').eq(0).text().trim()
      })
      // console.log(topicPair);
    })



    console.log(topics);
  })
  for (let i = 0; i < topicUrls.length; i++) {
    superagent.get(topicUrls[i]).end(function (err, res) {
      console.log('fetch' + topicUrls[i] + ' successful');
      ep.emit('topic_html', [topicUrls[i], res.text])
    })
  }

})
